import { Borrow } from '../entity/borrow.model';
import * as BorrowActions from '../actions/borrow.action';

const initBorrow=  {
  quota: [], // 背景数组
  config: [], // 借款picker数据
  selectedIndex: [], // 借款picker默认选择
  borrowModalStatus: 0, // 0代表未提交订单, 10提交审核
  progress: [], // 借款进度数组
  time: null, // 当前服务器时间
}
export function BorrowReducer(state: Borrow= initBorrow, action: BorrowActions.All){
  switch (action.type) {

    /*** 借款进度
     * 0 退出清空状态
     * */
    case BorrowActions.RESET_BORROW_STORE:
      return initBorrow;
    /*** 背景额度
     * 0.1 退出清空状态
     * */
    case BorrowActions.GET_USER_QUOTA_LIST:
      return {...state,
        ...action.payload,
      };

    /*** 获取借款进度
     * 1.1 查询用户借款进度条目成功
     * */
    case BorrowActions.GET_USER_BORROW_PROGRESS_SUCCESS:
      return {...state,
        borrowModalStatus: action.payload.state, // 更新为审核中状态。
        progress: [...action.payload.progress], // 更新为审核中状态。
        time: action.payload.time, // 当前服务器时间
      };

    /*** 获取借款进度
     * 1.2 查询用户借款进度条目为空
     * */
    case BorrowActions.GET_USER_BORROW_PROGRESS_FAILED:
      return {...state,
        borrowModalStatus: 0,  // 更新为借款状态。
        progress: [],  // 清空审核中数据。
      };

    /*** 获取借款配置
     * 2.1 默认
    * */
    case BorrowActions.GET_APP_BORROW_PICKER_DEFAULT:
      return {...state,
        ...action.payload,
        borrowModalStatus: 0,
        progress: [],
      };

    /*** 获取借款配置
     * 2.2用户额度
     * */
    case BorrowActions.GET_USER_BORROW_PICKER:
      return {...state,
        ...action.payload,
        borrowModalStatus: 0,
        progress: [],
      };

    /*** 获取借款配置
     * 3.1 借款进度状态修改
     * */
    case BorrowActions.UPDATE_BORROW_MODAL_STATUS:
      return {...state,
        borrowModalStatus: 10, // 更新为提交审核
        config: [], // 清除picker 缓存数据
        selectedIndex: [],
      };


    default:
      return state;
  }
}
